package tableprocessor;

import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfReader;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;

public class PDFFilesProcessor implements FileInterface
{

    public boolean isIam(String path) 
    {
        try
        {
            PdfReader pr = new PdfReader(path);
            pr.close();
            return true;
        }
        catch (IOException ex)
        {
          return false;
        }
    }
    public Iterator<Table> parse(String path)
    {
        List <Table> list = new ArrayList<Table>();
        list.add(getTable(path));
        return list.iterator();
    }
    public Table getTable(String path) 
    {
        try
        {
            Table t = new Table();
            PDDocument pddDocument = PDDocument.load(new File(path));
            PDFTextStripper textStripper = new PDFTextStripper();
            StringBuffer rez = new StringBuffer(textStripper.getText(pddDocument));
            pddDocument.close();
            StringTokenizer st = new StringTokenizer(rez.toString(), "\n");
            int i = 0; // for string
            while (st.hasMoreTokens()) {
                StringTokenizer st2 = new StringTokenizer(st.nextToken().toString());
                t.insertRow(i);
                while (st2.hasMoreTokens()) {
                    t.addToRow(i, st2.nextToken());
                }
                i++;
            }
            return t;
        } catch (IOException ex)
        {
            return null;
        }
    }

}

